package com.sk.weichat.ui.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.audio_x.VoicePlayer;
import com.sk.weichat.bean.collection.CollectionEvery;
import com.sk.weichat.db.InternationalizationHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.view.SelectionFrame;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class SelectCollectionActivity extends BaseActivity {
    public static final String SelectCollectionKey = "SelectCollectionKey";


    CollectionFragment mCollectionFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_collection);
        initActionBar();

        mCollectionFragment = new CollectionFragment();
        mCollectionFragment.setSelectAble(true);
        Bundle data = new Bundle();
        data.putBoolean("isItemEditAble", true);

        mCollectionFragment.setArguments(data);
        mCollectionFragment.setListener(new CollectionFragment.LoadListener() {
            @Override
            public void onLoading() {

            }

            @Override
            public void onLoadFinish() {
                if (mCollectionFragment.getData().size() == 0) {
                    findViewById(R.id.tv_send).setVisibility(View.GONE);
                    findViewById(R.id.view).setVisibility(View.GONE);
                } else {
                    findViewById(R.id.tv_send).setVisibility(View.VISIBLE);
                    findViewById(R.id.view).setVisibility(View.VISIBLE);
                    findViewById(R.id.tv_send).setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
                    findViewById(R.id.tv_send).setEnabled(false);
                }
            }
        });

        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, mCollectionFragment).commit();

        findViewById(R.id.tv_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                SelectionFrame dialog = new SelectionFrame(mContext);
                dialog.setSomething(null, mContext.getString(R.string.tip_confirm_send), new SelectionFrame.OnSelectionFrameClickListener() {
                    @Override
                    public void cancelClick() {

                    }

                    @Override
                    public void confirmClick() {
                        //
                        Intent result = new Intent();
                        List<CollectionEvery> list = mCollectionFragment.getData();
                        ArrayList<CollectionEvery> resultList = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            if (!list.get(i).isChecked)
                                continue;
                            resultList.add(list.get(i));
                        }
                        result.putParcelableArrayListExtra(SelectCollectionKey, resultList);
                        setResult(Activity.RESULT_OK, result);
                        finish();
                    }
                });
                dialog.show();

            }
        });

    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        TextView tvTitle = findViewById(R.id.tv_title_center);
        //  tvTitle.setText(InternationalizationHelper.getString("JX_MyCollection"));
        tvTitle.setText(InternationalizationHelper.getString("JX_Collection"));
    }

    @Override
    protected void onPause() {
        stopVoiceOrVideo();
        super.onPause();
    }

    private void stopVoiceOrVideo() {
        VoicePlayer.instance().stop();
        JCVideoPlayer.releaseAllVideos();
    }

    protected void sendable( boolean isSendable){
        if(isSendable){
            findViewById(R.id.tv_send).setBackgroundResource(R.drawable.shape_corner_26dp_solid_primary);
            findViewById(R.id.tv_send).setEnabled(true);
        }else{
            findViewById(R.id.tv_send).setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
            findViewById(R.id.tv_send).setEnabled(false);
        }
    }

}
